package cn.uc.db;

import java.util.Vector;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DBOperator {
    private Context mContext = null;
    private String mDbName;
    private DiaryDBHelper mHelper = null;
    private SQLiteDatabase mDb = null;
    
    private static Vector<Storable> gTables = new Vector<Storable>();
    
    private static final int DB_VERSION = 1;
    
    private static class DiaryDBHelper extends SQLiteOpenHelper{

        public DiaryDBHelper(Context aContext, String aDbName) {
            super(aContext, aDbName, null, DB_VERSION);
            // TODO Auto-generated constructor stub
        }

        //@Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            //db.execSQL(CREATE_ACCOUNT_TABLE);
            for (int i = 0; i < gTables.size(); i++) {
                String sql = "create table ";
                Storable storable = gTables.elementAt(i);
                sql += storable.storeTable();
                sql += "(";

                for (String sKey : storable.attributes().keySet()) {
                    sql += " ";
                    sql += sKey;
                    Storable.DBFieldType sdbType = storable.attributes().get(sKey);
                    if(sdbType == Storable.DBFieldType.FT_TEXT){
                        sql += " text";
                    }else if(sdbType == Storable.DBFieldType.FT_NUMBER){
                        sql += " integer";
                    }
                    
                    if(sKey.equals(storable.primaryKey())){
                        sql += (" PRIMARY KEY");
                    }
                }
                sql += ")";
                db.execSQL(sql);
            }
        }

        //@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
        
    }
    
    
    public DBOperator(Context aContext, String aDbName){
        mContext = aContext;
        mDbName = aDbName;
    }
    
    public static void setDBTables(Vector<Storable> aTables){
        gTables = aTables;
    }
    
    public void openDatabase()throws SQLException
    {
        mHelper = new DiaryDBHelper(mContext, mDbName);
        mDb = mHelper.getWritableDatabase();        
    }

    public void closeDatabase()
    {
        mHelper.close();
    }
    
    public void addDBObject(Storable aStorable){
        String sql = "";
        
    }
    
    public void deleteDBObject(Storable aStorable){
        
    }
    
    public void upadteDBObject(Storable aStorable){
        
    }
    
    public void getDBObjects(Class<Storable> aClass){
        try {
            Storable aStorable = aClass.newInstance();
            Cursor cursor=  mDb.rawQuery("select * from person", null);
            while(cursor.moveToNext()){
                
            }
        } catch (IllegalAccessException e) {
            // XXX Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // XXX Auto-generated catch block
            e.printStackTrace();
        }
    }
}
